package com.ch.dsms_backend.controller;

import com.ch.dsms_backend.entity.AUser;
import com.ch.dsms_backend.entity.Order;
import com.ch.dsms_backend.repository.UserAndOrderAndOutRepository;
import com.ch.dsms_backend.service.UserAndOrderAndOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
public class UserAndOrderAndOutController{
    @Autowired
    private UserAndOrderAndOutService userAndOrderAndOutService;

    @Autowired
    private UserAndOrderAndOutRepository userAndOrderAndOutRepository;

    @CrossOrigin //跨域访问
    @RequestMapping("/selectOrderDetail")
    public List<Map<String,Object>> selectOrderDetail(Integer oid){
        return userAndOrderAndOutRepository.selectOrderDetail(oid);
    }

    @CrossOrigin //跨域访问
    @RequestMapping("/selectOrderByUserName")
    public List<Map<String,Object>> selectOrderByUserName(String username){
        String likeUsername="%"+username+"%";
        return userAndOrderAndOutRepository.selectOrderByUserName(likeUsername);
    }

    @CrossOrigin //跨域访问
    @RequestMapping("/getOrderAmountAndOrderdate")
    public List<Map<String,Object>> getOrderAmountAndOrderdate(){
       return userAndOrderAndOutRepository.getOrderAmountAndOrderdate();
    }


    @CrossOrigin //跨域访问
    @RequestMapping("/selectAllOrders")
    public List<Map<String,Object>> selectAllOrders(HttpSession session){
        return userAndOrderAndOutService.selectAllOrders();
    }

    @CrossOrigin //跨域访问
    @RequestMapping("/selectPaidOrders")
    public List<Map<String,Object>> selectPaidOrders(HttpSession session){
        return userAndOrderAndOutRepository.selectOrdersByStatus(1);
    }

    @CrossOrigin //跨域访问
    @RequestMapping("/selectUnpaidOrders")
    public List<Map<String,Object>> selectUnpaidOrders(HttpSession session){
        return userAndOrderAndOutRepository.selectOrdersByStatus(0);
    }


    @CrossOrigin //跨域访问
    @RequestMapping("/loginOut")
    public String loginOut(@ModelAttribute("aUser") AUser aUser, HttpSession session){
        session.invalidate();
        return "admin/login";
    }

    @CrossOrigin //跨域访问
    @RequestMapping("/selectUser")
    public String selectUser(Model model,int currentPage){
        return userAndOrderAndOutService.selectUser(model,currentPage);
    }

    @CrossOrigin //跨域访问
    @RequestMapping("/deleteUser")
    @ResponseBody
    public String deleteUser(Model model,int id){
        return userAndOrderAndOutService.deleteUser(model,id);
    }

    @CrossOrigin //跨域访问
    @RequestMapping("/selectOrder")
    public String selectOrder(Model model,int currentPage){
        return userAndOrderAndOutService.selectOrder(model,currentPage);
    }



}
